home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / Label.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  6.6 KB  |  246 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Label.java    1.41 98/08/31
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.LabelPeer;
  17.  
  18. /**
  19.  * A <code>Label</code> object is a component for placing text in a
  20.  * container. A label displays a single line of read-only text.
  21.  * The text can be changed by the application, but a user cannot edit it
  22.  * directly.
  23.  * <p>
  24.  * For example, the code . . .
  25.  * <p>
  26.  * <hr><blockquote><pre>
  27.  * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
  28.  * add(new Label("Hi There!"));
  29.  * add(new Label("Another Label"));
  30.  * </pre></blockquote><hr>
  31.  * <p>
  32.  * produces the following label:
  33.  * <p>
  34.  * <img src="doc-files/Label-1.gif"
  35.  * ALIGN=center HSPACE=10 VSPACE=7>
  36.  *
  37.  * @version    1.41, 08/31/98
  38.  * @author     Sami Shaio
  39.  * @since       JDK1.0
  40.  */
  41. public class Label extends Component {
  42.  
  43.     static {
  44.         /* ensure that the necessary native libraries are loaded */
  45.     Toolkit.loadLibraries();
  46.         initIDs();
  47.     }
  48.  
  49.     /**
  50.      * Indicates that the label should be left justified.
  51.      */
  52.     public static final int LEFT     = 0;
  53.  
  54.     /**
  55.      * Indicates that the label should be centered.
  56.      */
  57.     public static final int CENTER     = 1;
  58.  
  59.     /**
  60.      * Indicates that the label should be right justified.
  61.      * @since   JDK1.0t.
  62.      */
  63.     public static final int RIGHT     = 2;
  64.  
  65.     /**
  66.      * The text of this label.
  67.      * This text can be modified by the program
  68.      * but never by the user.
  69.      *
  70.      * @serial
  71.      * @see getText()
  72.      * @see setText()
  73.      */
  74.     String text;
  75.  
  76.     /**
  77.      * The label's alignment.  The default alignment is set
  78.      * to be left justified.
  79.      *
  80.      * @serial
  81.      * @see getAlignment()
  82.      * @see setAlignment()
  83.      */
  84.     int       alignment = LEFT;
  85.  
  86.     private static final String base = "label";
  87.     private static int nameCounter = 0;
  88.  
  89.     /*
  90.      * JDK 1.1 serialVersionUID
  91.      */
  92.      private static final long serialVersionUID = 3094126758329070636L;
  93.  
  94.     /**
  95.      * Constructs an empty label.
  96.      * The text of the label is the empty string <code>""</code>.
  97.      */
  98.     public Label() {
  99.     this("", LEFT);
  100.     }
  101.  
  102.     /**
  103.      * Constructs a new label with the specified string of text,
  104.      * left justified.
  105.      * @param text the string that the label presents.
  106.      *        A <code>null</code> value
  107.      *        will be accepted without causing a NullPointerException
  108.      *        to be thrown.
  109.      */
  110.     public Label(String text) {
  111.         this(text, LEFT);
  112.     }
  113.  
  114.     /**
  115.      * Constructs a new label that presents the specified string of
  116.      * text with the specified alignment.
  117.      * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
  118.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  119.      * @param text the string that the label presents.
  120.      *        A <code>null</code> value
  121.      *        will be accepted without causing a NullPointerException
  122.      *        to be thrown.
  123.      * @param     alignment   the alignment value.
  124.      */
  125.     public Label(String text, int alignment) {
  126.     this.text = text;
  127.     setAlignment(alignment);
  128.     }
  129.  
  130.     /**
  131.      * Construct a name for this component.  Called by getName() when the
  132.      * name is <code>null</code>.
  133.      */
  134.     String constructComponentName() {
  135.         synchronized (getClass()) {
  136.         return base + nameCounter++;
  137.     }
  138.     }
  139.  
  140.     /**
  141.      * Creates the peer for this label.  The peer allows us to
  142.      * modify the appearance of the label without changing its
  143.      * functionality.
  144.      */
  145.     public void addNotify() {
  146.         synchronized (getTreeLock()) {
  147.         if (peer == null)
  148.             peer = getToolkit().createLabel(this);
  149.         super.addNotify();
  150.     }
  151.     }
  152.  
  153.     /**
  154.      * Gets the current alignment of this label. Possible values are
  155.      * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
  156.      * <code>Label.CENTER</code>.
  157.      * @see        java.awt.Label#setAlignment
  158.      */
  159.     public int getAlignment() {
  160.     return alignment;
  161.     }
  162.  
  163.     /**
  164.      * Sets the alignment for this label to the specified alignment.
  165.      * Possible values are <code>Label.LEFT</code>,
  166.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  167.      * @param      alignment    the alignment to be set.
  168.      * @exception  IllegalArgumentException if an improper value for
  169.      *                          <code>alignment</code> is given.
  170.      * @see        java.awt.Label#getAlignment
  171.      */
  172.     public synchronized void setAlignment(int alignment) {
  173.     switch (alignment) {
  174.       case LEFT:
  175.       case CENTER:
  176.       case RIGHT:
  177.         this.alignment = alignment;
  178.             LabelPeer peer = (LabelPeer)this.peer;
  179.         if (peer != null) {
  180.         peer.setAlignment(alignment);
  181.         }
  182.         return;
  183.     }
  184.     throw new IllegalArgumentException("improper alignment: " + alignment);
  185.     }
  186.  
  187.     /** 
  188.      * Gets the text of this label. 
  189.      * @return     the text of this label, or <code>null</code> if 
  190.      *             the text has been set to <code>null</code>.
  191.      * @see        java.awt.Label#setText
  192.      */
  193.     public String getText() {
  194.     return text;
  195.     }
  196.  
  197.     /**
  198.      * Sets the text for this label to the specified text.
  199.      * @param      text the text that this label displays. If 
  200.      *             <code>text</code> is <code>null</code>, it is 
  201.      *             treated for display purposes like an empty 
  202.      *             string <code>""</code>.
  203.      * @see        java.awt.Label#getText
  204.      */
  205.     public void setText(String text) {
  206.         boolean testvalid = false;
  207.  
  208.     synchronized (this) {
  209.         if (text != this.text && (this.text == null ||
  210.                       !this.text.equals(text))) {
  211.             this.text = text;
  212.         LabelPeer peer = (LabelPeer)this.peer;
  213.         if (peer != null) {
  214.             peer.setText(text);
  215.         }
  216.         testvalid = true;
  217.         }
  218.     }
  219.  
  220.     // This could change the preferred size of the Component.
  221.     if (testvalid && valid) {
  222.         invalidate();
  223.     }
  224.     }
  225.  
  226.     /**
  227.      * Returns the parameter string representing the state of this
  228.      * label. This string is useful for debugging.
  229.      * @return     the parameter string of this label.
  230.      */
  231.     protected String paramString() {
  232.     String str = ",align=";
  233.     switch (alignment) {
  234.       case LEFT:   str += "left"; break;
  235.       case CENTER: str += "center"; break;
  236.       case RIGHT:  str += "right"; break;
  237.     }
  238.     return super.paramString() + str + ",text=" + text;
  239.     }
  240.  
  241.     /**
  242.      * Initialize JNI field and method IDs
  243.      */
  244.     private static native void initIDs();
  245. }
  246.